const Service = require('egg').Service;
const _ = require('lodash')

class AppService extends Service {
  // 新增流程记录
  async add(model) {
    const { RepairWorkflow } = this.app.model;

    const data = RepairWorkflow.build(model);
    const ret = await RepairWorkflow.create(model);
    return ret.get({ plain: true });
  }

  //获取流程记录
  async getWorkflow(workflow_id) {
    const { app: { model: { RepairWorkflow } } } = this
    let data = await RepairWorkflow.findAll({
      attributes: ['remark', 'step', 'remark', 'from_username', 'created_at', 'to_username'],
      order: [
        ["created_at", "ASC"]
      ],
      where: {
        workflow_id
      },
      raw: true,
    })

    //数据简单转换一下
    return _.map(data, (d) => {
      return {
        username: d.from_username,
        content: [
          d.to_username ? `${d.step}给${d.to_username}` : d.step,
          d.remark,
        ],
        time: d.created_at,
      }
    });
  }
}
module.exports = AppService
